<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="活动类型模板" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择活动类型，默认全部"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="option in typeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:rank:config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:rank:config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:rank:config:delete']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置id" prop="id" width="80" />
      <el-table-column label="活动描述" prop="descption" width="120" />
      <el-table-column label="活动类型" prop="type" :show-overflow-tooltip="true" width="80"/>
      <el-table-column label="是否开启" prop="haveClose" :show-overflow-tooltip="true" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.haveClose === false">开启</span>
          <span v-if="scope.row.haveClose === true">关闭</span>
        </template>
      </el-table-column>
      <el-table-column label="礼物id" prop="giftId" :show-overflow-tooltip="true" width="160" />
      <el-table-column label="活动开始时间" align="center" prop="activityStartTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="activityEndTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:rank:config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:rank:config:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="活动类型" prop="type">
          <el-select v-model="activityType">
            <el-option
              v-for="option in typeOptions"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="活动描述" prop="descption">
          <el-input v-model.number="description" placeholder="请输入活动描述" />
        </el-form-item>

        <el-form-item label="礼物id" prop="giftId">
          <el-input v-model.number="giftId" placeholder="请输入礼物id" />
        </el-form-item>

        <el-form-item label="活动时间">
          <el-date-picker
            v-model="value2"
            format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期" @change="changeTime">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getActivity, updateActivity, addActivity, deleteActivity } from "@/api/activity/rank/config";
import { listEnums } from "@/api/activity/rank/configSports";

export default {
  name: "RankConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 条件列表列表
      menuOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type:"",
        activityStartTime:undefined,
        activityEndTime:undefined,
        createTime:undefined,
        newActivityStartTime:undefined,
        newActivityEndTime:undefined,
      },
      // 表单参数
      form: {},
      typeOptions:[],
      value2: [this.getTime(new Date()), this.getTime(new Date())],
      description:'',
      giftId:undefined,
      activityType:undefined,
      activityId:undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(
        response => {
          this.configList = response.records;
          this.total = response.total;
          this.loading = false;
        }
      );
      listEnums().then(response =>{
        this.typeOptions = response.data
      })
    },
    changeTime(val){
      if (val !== null){
        this.val = Array.isArray(this.val) ? this.val : [];
        this.value2 = [this.getTime(new Date(val[0])),this.getTime(new Date(val[1]))]
      } else {
        this.value2 = [this.getTime(new Date()),this.getTime(new Date())]
      }
    },
    getTime(time){
      let year = time.getFullYear()
      let month = this.getTimeByLength(time.getMonth() + 1);
      let day = this.getTimeByLength(time.getDate())
      let hour = this.getTimeByLength(time.getHours())
      let minute = this.getTimeByLength(time.getMinutes())
      let second = this.getTimeByLength(time.getSeconds())
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    },
    getTimeByLength(timeUnit){
      return timeUnit < 10 ? '0' + timeUnit :timeUnit
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu !== undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        id: undefined,
        type: undefined,
        haveClose:false,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log("query:===>",this.queryParams.type)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getActivity(id).then(response => {
        this.form = response.data;
        let start = response.data.activityStartTime
        let end = response.data.activityEndTime
        this.value2 = [start, end]
        this.description = response.data.descption
        this.activityType = "" + response.data.type
        this.giftId = response.data.giftId
        this.activityId = response.data.id
        this.open = true;
        this.title = "修改配置";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除活动编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return deleteActivity(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        this.value2 = Array.isArray(this.value2) ? this.value2 : [];
        let startTime = this.value2[0];
        let endTime = this.value2[1];
        let data = {
          "start": startTime,
          "end": endTime,
          "giftId":this.giftId,
          "activityType": this.activityType,
          "description": this.description,
          "id": this.activityId
        }
        if (valid) {
          if (this.form.id !== undefined) {
            updateActivity(data).then(response =>{
              this.msgSuccess("修改成功");
              this.open = false;
              this.resetData();
              this.getList();
            })
          } else {
            addActivity(data).then(response => {
              console.log("data;;;;====>",data)
              this.msgSuccess("新增成功");
              this.open = false;
              this.resetData();
              this.getList();
            });
          }
        }


      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    resetData() {
      this.activityId = undefined
      this.description = undefined
      this.activityType = undefined
      this.activityStartTime = undefined
      this.activityEndTime = undefined
    }
  }
};
</script>
